package cn.pli.bike.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;


import com.framemodule.base.BaseActivityPresenter;
import com.framemodule.base.netstatus.NetUtils;
import com.framemodule.utils.Logger;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import cn.pli.bike.bean.HttpResult;
import cn.pli.bike.model.UserModel;
import cn.pli.bike.model.subscribers.ProgramSubscriber;
import cn.pli.bike.service.ConnectService;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends BaseActivityPresenter<SplashDelegate> {
    public static final int SPLASH_SHOW_TIME = 2;       //开屏动画展示时间
    List<String> images;
    int offset = 0;
    Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                readyGoThenKill(MainActivity.class);
            }
        }, SPLASH_SHOW_TIME * 1000);
        UserModel.getStartPage(new ProgramSubscriber<HttpResult<List<String>>>() {
            @Override
            public void onNext(HttpResult<List<String>> stringHttpResult) {
                if (timer != null) {
                    timer.cancel();
                }
                images = stringHttpResult.getData();
                if (images.size() > 0) {
                    ImageLoader.getInstance().displayImage(images.get(offset), viewDelegate.getImageView());
                }
                int count = 1;
                if (images == null || images.size() == 0) {
                    count = 1;
                } else {
                    count = images.size();
                }

                Observable.interval(SPLASH_SHOW_TIME, SPLASH_SHOW_TIME, TimeUnit.SECONDS).take(count).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (images == null || images.size() == 0) {
                            readyGoThenKill(MainActivity.class);
                            return;
                        }
                        if (offset == images.size() - 1) {
                            readyGoThenKill(MainActivity.class);
                            return;
                        }
                        offset++;
                        if (viewDelegate != null) {
                            ImageLoader.getInstance().displayImage(images.get(offset), viewDelegate.getImageView());
                        }
                    }
                });
            }

        });
        checkVersion();


    }

    @Override
    protected void initModel() {

    }

    @Override
    protected void onResume() {
        super.onResume();


    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    public void checkVersion() {
        Intent intent = new Intent(this, ConnectService.class);
        intent.setType(ConnectService.TYPE_CHECK_UPDATE);
        startService(intent);
    }
}
